package ru.ipo.dces.plugins.multivariateplugin;


import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.net.URL;

public class ButtonFactory {
    private static URL buttonUpResource = ButtonFactory.class.getResource("resources/button_up.png");
    private static URL buttonDownResource = ButtonFactory.class.getResource("resources/button_down.png");
    private static URL buttonDownPressedResource = ButtonFactory.class.getResource("resources/button_down_pressed.png");
    private static URL buttonOkResource = ButtonFactory.class.getResource("resources/button_ok.png");
    private static URL buttonCancelResource = ButtonFactory.class.getResource("resources/button_cancel.png");

    private static final ImageIcon BUTTON_DOWN_PRESSED = new ImageIcon(buttonDownPressedResource);
    private static final ImageIcon BUTTON_DOWN = new ImageIcon(buttonDownResource);
    private static final ImageIcon BUTTON_UP = new ImageIcon(buttonUpResource);
    private static final ImageIcon BUTTON_OK = new ImageIcon(buttonOkResource);
    private static final ImageIcon BUTTON_CANCEL = new ImageIcon(buttonCancelResource);



    public static JRadioButton getDecoratedJRadioButton() {
        JRadioButton button = new JRadioButton();
        decorate(button);
        return button;
    }

    public static JCheckBox getDecoratedJCheckButton() {
        JCheckBox button = new JCheckBox();
        decorate(button);
        return button;
    }

    private static void decorate(final JToggleButton button) {
        button.setIcon(BUTTON_UP);
        button.setSelectedIcon(BUTTON_OK);
        button.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent event) {
                if (button.isSelected())
                    button.setPressedIcon(null);
                else
                    button.setPressedIcon(BUTTON_DOWN);
            }
        });
    }
}
